package com.test.myfreemark;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class MyCodeFrame extends JFrame {
	Container container;
	JTextField text1, text2, text3, text4,text5;
	JTable table;
	JScrollPane jsp;
	DefaultTableModel tableModel;
	MyItemItemListener mil = new MyItemItemListener();
	Set<String> tableNames = new HashSet<String>();
	List<String> AlltableNames;
	String FileSavePath;
	Set<String> FileTypes = new HashSet<String>();

	public MyCodeFrame() throws ClassNotFoundException, InstantiationException,
			IllegalAccessException, UnsupportedLookAndFeelException {
		// 更换皮肤
		String UIM = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
		UIManager.setLookAndFeel(UIM);
		SwingUtilities.updateComponentTreeUI(this);
		container = getContentPane();
		setSize(600, 500);
		setLayout(null);// 空布局

		JLabel lable1 = new JLabel("连接字符：");
		lable1.setBounds(10, 20, 80, 25);
		container.add(lable1);
		text1 = new JTextField(
				"jdbc:sqlserver://127.0.0.1:1433;databaseName=renbanghome;");
		text1.setBounds(110, 20, 300, 25);
		container.add(text1);
		final JComboBox jcb = new JComboBox();
		jcb.setBounds(440, 20, 100, 30);
		jcb.addItem("SQLSERVER");
		jcb.addItem("ORACLE");
		jcb.addItem("MYSQL");
		jcb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (jcb.getSelectedItem().toString().equals("ORACLE")) {
					MyFreeMarker.ftlDir=FtlDir.ORACLE.getDir();
					DBUtils.DRIVER = DBUtils.DRIVERORACLE;
					DBUtils.tableNamesSQL = DBUtils.tableNamesForORACLE;
					DBUtils.TableClums = DBUtils.TableClumsORA;
				} else if (jcb.getSelectedItem().toString().equals("MYSQL")) {
					DBUtils.DRIVER = DBUtils.DRIVERMYSQL;
					MyFreeMarker.ftlDir=FtlDir.MYSQL.getDir();
					String database = text1.getText().substring(
							text1.getText().lastIndexOf("/") + 1,
							text1.getText().length());
					DBUtils.tableNamesSQL = DBUtils.tableNamesForMYSQL
							+ database + "';";
					System.out.println(DBUtils.tableNamesSQL);
					DBUtils.TableClums = DBUtils.TableClumsSAndM;
				} else if (jcb.getSelectedItem().toString().equals("SQLSERVER")) {
					MyFreeMarker.ftlDir=FtlDir.SQLSERVER.getDir();
					DBUtils.DRIVER = DBUtils.DRIVERSELSERVER;// 驱动类
					DBUtils.tableNamesSQL = DBUtils.tableNamesForSQLSERVER;// 查询表名sql
					DBUtils.TableClums = DBUtils.TableClumsSAndM;// 查询字段名和类型sql
				}

			}
		});
		;
		container.add(jcb);
		JLabel lable2 = new JLabel("用户名：");
		lable2.setBounds(10, 50, 80, 25);
		container.add(lable2);
		text2 = new JTextField("sa");
		text2.setBounds(110, 50, 120, 25);
		container.add(text2);
		JLabel lable3 = new JLabel("密码：");
		lable3.setBounds(10, 80, 80, 25);
		container.add(lable3);
		text3 = new JTextField("1234");
		text3.setBounds(110, 80, 120, 25);
		container.add(text3);
		JButton button1 = new JButton("加载数据库");
		button1.setBounds(250, 50, 120, 25);
		tableModel = new DefaultTableModel();
		table = new JTable();
		jsp = new JScrollPane(table);
		jsp.setBounds(10, 130, 400, 150);
		container.add(jsp);
		button1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DBUtils.URL = text1.getText();
				DBUtils.USER = text2.getText();
				DBUtils.PASS = text3.getText();
				try {
					tableNames.clear();
					AlltableNames = DBUtils.getTableNames(null);
					Vector<Object> datas = new Vector<Object>();
					Vector<Object> names = new Vector<Object>();
					names.add(0, "表名");
					names.add(1, "选择");
					for (int i = 0; i < AlltableNames.size(); i++) {
						Vector<Object> data = new Vector<Object>();
						data.add(AlltableNames.get(i));
						data.add("");
						datas.add(data);
					}
					tableModel.setDataVector(datas, names);
					table.setModel(tableModel);
					JCheckBox jc = new JCheckBox();
					table.getColumnModel().getColumn(1)
							.setCellEditor(new DefaultCellEditor(jc));
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		container.add(button1);

		JButton button2 = new JButton("选择保存位置");
		button2.setBounds(250, 80, 120, 25);
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser();
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				container.add(jfc);
				jfc.showDialog(new JLabel(), "选择");
				File file = jfc.getSelectedFile();
				if (file.isDirectory()) {
					FileSavePath = file.getAbsolutePath();
				} else if (file.isFile()) {
				}
				System.out.println(jfc.getSelectedFile().getName());
			}
		});
		container.add(button2);

		JLabel lable4 = new JLabel("基础包名");
		lable4.setBounds(10, 300, 80, 25);
		container.add(lable4);
		text4 = new JTextField();
		text4.setBounds(80, 300, 200, 25);
		container.add(text4);

		JLabel lable5 = new JLabel("分包");
		lable5.setBounds(300, 300, 40, 25);
		container.add(lable5);
		text5 = new JTextField("");
		text5.setBounds(330, 300, 100, 25);
		container.add(text5);
		
		JLabel lablebean = new JLabel("bean:");
		lablebean.setBounds(10, 330, 80, 25);
		container.add(lablebean);
		JCheckBox checkbean = new JCheckBox("BEAN");
		checkbean.setBounds(40, 330, 20, 25);
		checkbean.setSelected(true);
		container.add(checkbean);
		checkbean.addItemListener(mil);

		JLabel labledao = new JLabel("dao:");
		labledao.setBounds(70, 330, 80, 25);
		container.add(labledao);
		JCheckBox checkdao = new JCheckBox("DAO");
		checkdao.setBounds(95, 330, 20, 25);
		checkdao.addItemListener(mil);
		checkdao.setSelected(true);
		container.add(checkdao);

		JLabel lablemapper = new JLabel("mapper:");
		lablemapper.setBounds(120, 330, 80, 25);
		container.add(lablemapper);
		JCheckBox checkmapper = new JCheckBox("MAPPER");
		checkmapper.setBounds(165, 330, 20, 25);
		checkmapper.setSelected(true);
		checkmapper.addItemListener(mil);
		container.add(checkmapper);

		JLabel lableservice = new JLabel("service:");
		lableservice.setBounds(190, 330, 80, 25);
		container.add(lableservice);
		JCheckBox checkservice = new JCheckBox("SERVICE");
		checkservice.setBounds(230, 330, 20, 25);
		checkservice.addItemListener(mil);
		container.add(checkservice);

		JLabel lableaction = new JLabel("action:");
		lableaction.setBounds(255, 330, 80, 25);
		container.add(lableaction);
		JCheckBox checkaction = new JCheckBox("ACTION");
		checkaction.setBounds(290, 330, 20, 25);
		container.add(checkaction);
		checkaction.addItemListener(mil);

		JLabel lablejsplist = new JLabel("jsplist:");
		lablejsplist.setBounds(315, 330, 80, 25);
		container.add(lablejsplist);
		JCheckBox checkajsplist = new JCheckBox("JSPLIST");
		checkajsplist.setBounds(350, 330, 20, 25);
		checkajsplist.addItemListener(mil);
		container.add(checkajsplist);

		JLabel lablejspform = new JLabel("jspform:");
		lablejspform.setBounds(375, 330, 80, 25);
		container.add(lablejspform);
		JCheckBox checkajspform = new JCheckBox("JSPFORM");
		checkajspform.setBounds(420, 330, 20, 25);
		checkajspform.addItemListener(mil);
		container.add(checkajspform);

		FileTypes.add("BEAN");
		FileTypes.add("DAO");
		FileTypes.add("MAPPER");
		JButton button3 = new JButton("开始生成");
		button3.setBounds(480, 400, 80, 25);
		button3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(FileSavePath==null||FileSavePath.equals("")){
					JOptionPane.showMessageDialog(null, "未选择保存路径");
					return;
				}
				
				for (int i = 0; i < tableModel.getRowCount(); i++) {
					String booleanStr = tableModel.getValueAt(i, 1) + "";
					if (booleanStr.equals("true")) {
						tableNames.add(AlltableNames.get(i));
					} else if (booleanStr.equals("false")) {
						tableNames.remove(i);
					}
				}
				try {
					String basedirpath = text4.getText() == null
							|| text4.getText().equals("") ? "com.renbang"
							: text4.getText();
					String detailPackge = text5.getText() == null
							|| text5.getText().equals("") ? ""
							: "."+text5.getText();
					
					System.out.println(basedirpath);
					MyFreeMarker.CreateCode(basedirpath, FileTypes, tableNames,
							FileSavePath,detailPackge);
					JOptionPane.showMessageDialog(null, "生成完毕");
				} catch (Exception e1) {
					e1.printStackTrace();
				}

			}
		});
		container.add(button3);
		setDefaultCloseOperation(3);
		isFocusableWindow();
		setLocationRelativeTo(null);
	}

	public static void main(String[] args) {
		try {
			new MyCodeFrame().setVisible(true);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
	}

	class MyItemItemListener implements ItemListener {
		JCheckBox jCheckBox;

		@Override
		public void itemStateChanged(ItemEvent e) {
			jCheckBox = (JCheckBox) e.getSource();
			if (jCheckBox.isSelected()) {
				FileTypes.add(jCheckBox.getText().trim());
			} else {
				FileTypes.remove(jCheckBox.getText().trim());
			}
		}
	}

}
